package com.example.demo2.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.demo2.entity.Course;
import com.example.demo2.entity.User;
import com.example.demo2.service.UserService;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired(required = false)
    private UserService userService;

    @GetMapping("/login")
    public String findByUserId(HttpSession session, @Param("userId") Integer userId, @Param("userPwd") String userPwd){
        userPwd = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        User users = userService.findByUserId(userId,userPwd);
        Gson  gson = new Gson();
        session.setAttribute("uid",users.getUserId());
        return gson.toJson(users);
    }

    @PostMapping("/register")
    public Map<String, Object> insert_user(@RequestParam("userId") String userId,
                                           @RequestParam("userName") String userName,
                                           @RequestParam("userPwd") String userPwd) {
        logger.info("Received request to register user: {}, {}, {}", userId, userName, userPwd);
        Map<String, Object> response = new HashMap<>();

        if (userService.userExists(userId)) {
            response.put("success", false);
            response.put("message", "存在重复工号请重新输入");
            logger.info("User ID already exists: {}", userId);
            return response;
        }

        // 对用户密码进行 MD5 加密
        userPwd = DigestUtils.md5DigestAsHex(userPwd.getBytes());

        // 创建 User 对象并设置属性
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserPwd(userPwd);

        // 调用服务层方法插入用户
        boolean isSuccess = userService.insert_user(user);
        response.put("success", isSuccess);
        logger.info("User registration success: {}", isSuccess);
        return response;
    }
    @GetMapping("/findByUserId_A")
    public String findByUserId_A(@Param("userId") Integer userId) {
        List<User> findByUserId_A = userService.findByUserId_A(userId);
        Gson gson = new Gson();
        return gson.toJson(findByUserId_A);
    }

    @RequestMapping("/upDateUserPwd")
    public boolean upDateUserPwd(@Param("userId") Integer userId,
                                 @Param("userPwd") String userPwd){
        userPwd =  DigestUtils.md5DigestAsHex(userPwd.getBytes());
        return userService.upDateUserPwd(userId,userPwd);
    }
    @RequestMapping("/upDateUserInformation")
    public boolean upDateUserInformation(@Param("userId") Integer userId,
                                         @Param("userName") String userName,
                                         @Param("userEmail") String userEmail,
                                         @Param("userBrief") String userBrief){
        return userService.upDateUserInformation(userId,userName,userEmail,userBrief);
    }
    @GetMapping("/forgetPwd")
    public String forgetPwd(@Param("userId") Integer userId,
                            @Param("userName") String userName,
                            @Param("userQuestion") String userQuestion){
        List<User> forgetPwd = userService.forgetPwd(userId,userName,userQuestion);
        Gson gson = new Gson();
        return gson.toJson(forgetPwd);

    }

}
